package com.primary.controller;

import com.primary.common.Result;
import com.primary.controller.request.DonationcarsReqeust;
import com.primary.entity.Donationcars;
import com.primary.service.DonationcarsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/cars")
public class DonationcarsController {

    @Autowired
    DonationcarsService donationcarsService;

    @RequestMapping(value = "/page",method = {RequestMethod.GET})
    public Result page(DonationcarsReqeust donationcarsReqeust){
        return Result.success(donationcarsService.queryByPage(donationcarsReqeust));
    }

    @RequestMapping(value = "/addOne", method = {RequestMethod.POST})
    public Result insertOneUser(@RequestBody Donationcars donationcars) {
        if (donationcars.getLicense() != null) {
            return donationcarsService.insertOne(donationcars) > 0 ?
                    Result.success() : Result.error("-114","插入错误");
        } else {
            return Result.error("请输入所有信息");
        }
    }

    @ResponseBody
    @RequestMapping(value = "/updateOne",method = {RequestMethod.PUT})
    public Result updateOne(@RequestBody Donationcars donationcars){
        if (donationcars.getLicense() != null){
            return donationcarsService.updateOneById(donationcars) > 0 ?
                    Result.success() : Result.error("更新失败!");
        } else {
            return Result.error("信息不能为空！");
        }
    }

    @RequestMapping(value = "/deleteOne/{id}",method = {RequestMethod.DELETE})
    public Result deleteById(@PathVariable Integer id){
        return null == id ? Result.error("ID不能为空") :
                (donationcarsService.deleteById(id) ? Result.success():Result.error("删除失败"));
    }
}
